//
//  PopoverBase.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-22.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "PopoverBase.h"

@implementation PopoverBase

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        if (AppSession.popController == nil) {
            AppSession.popController = [[UIPopoverController alloc] initWithContentViewController:self];
//            [AppSession.popController setValue:[NSNumber numberWithInt:3] forKey:@"popoverBackgroundStyle"];//改变边框颜色一 调用私有api
            
//            if ([AppSession.popController respondsToSelector:@selector(popoverBackgroundViewClass)]) {//修改背景二
////                AppSession.popController.popoverBackgroundViewClass = [CustomPopoverBackgroundView class];
//            }
        }
        
        if (AppSession.popController.popoverVisible) {
            [AppSession.popController dismissPopoverAnimated:NO];
        }
        
        [AppSession.popController setContentViewController:self];
        AppSession.popController.delegate=self;
        AppSession.popController.popoverContentSize = self.view.frame.size;//设置popoverview的内容大小
    }
    return self;
}

- (void)PopOverFromRect:(CGRect)rect inView:(UIView *)view directions:(UIPopoverArrowDirection)arrowDirections{
    [AppSession.popController presentPopoverFromRect:rect inView:view permittedArrowDirections:arrowDirections animated:YES]; 
}
- (void)PopOverFromBarButtonItem:(UIBarButtonItem *)item directions:(UIPopoverArrowDirection)arrowDirections{
    [AppSession.popController presentPopoverFromBarButtonItem:item permittedArrowDirections:arrowDirections animated:YES];
}

#pragma mark - UIPopover delegate
//---called when the user clicks outside the popover view---
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popOverController {
    MyNSLog(@"PopoverBase to be dismissed");
    return YES;
}
//---called when the popover view is dismissed---
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popOverController {
    MyNSLog(@"PopoverBase dismissed");  
}

#pragma mark - View lifecycle
- (void)viewDidLoad{
    [super viewDidLoad];
}
-(void) dealloc{
    MyNSLog(@"PopoverBase is deallocing...");
    [super dealloc];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}
- (void)viewDidUnload{
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
@end
